package com.example.ch03_2;


import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static android.widget.ImageView.ScaleType.FIT_XY;

public class MainActivity extends AppCompatActivity {

    private TextView tv_sum,tv_intro;
    private ImageView iv;
    private Button btn_xrq,btn_xyc,btn_xrk;
    private String sum;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initview();

        try{
            //读取information文件
            InputStream is=this.getResources().openRawResource(R.raw.information);
            List<Flower> flowers=flowerservice.getFromXML(is);

            tv_intro.setVisibility(View.VISIBLE);
            Flower flower=(Flower)flowers.get(2);
            iv.setImageResource(R.drawable.xrk);
            tv_sum.setText("  "+flower.getSum());

            btn_xrk.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    tv_intro.setVisibility(View.VISIBLE);
                    Flower flower=(Flower)flowers.get(2);
                    iv.setImageResource(R.drawable.xrk);
                    tv_sum.setText("  "+flower.getSum());
                }
            });
            btn_xyc.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    tv_intro.setVisibility(View.VISIBLE);
                    Flower flower=(Flower)flowers.get(1);
                    iv.setImageResource(R.drawable.xyc);
                    tv_sum.setText("  "+flower.getSum());
                }
            });
            btn_xrq.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    tv_intro.setVisibility(View.VISIBLE);
                    Flower flower=(Flower)flowers.get(0);
                    iv.setImageResource(R.drawable.xrq);
                    iv.setScaleType(FIT_XY);
                    tv_sum.setText("  "+flower.getSum());
                }
            });

        }catch (Exception e){
            e.printStackTrace();
            Toast.makeText(this,"解析失败",Toast.LENGTH_SHORT).show();
            Log.i("a","解析失败");
        }

    }


    private void initview(){
        tv_intro=(TextView)findViewById(R.id.tv_introduct);
        tv_intro.setVisibility(View.GONE);
        tv_sum=(TextView)findViewById(R.id.tv_sum);
        iv=(ImageView)findViewById(R.id.iv);
        btn_xrq=(Button)findViewById(R.id.btn_xrq);
        btn_xyc=(Button)findViewById(R.id.btn_xyc);
        btn_xrk=(Button)findViewById(R.id.btn_xrk);
    }

}